package com.bwton.msx.ccb.service.user;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;

import io.reactivex.Observable;
import io.reactivex.subjects.BehaviorSubject;

/**
 * 用户管理服务：维护、更新、查询用户信息
 */
public class UserManager {
    private Context mContext;
    private User mUser = null;
    private Gson gson = new Gson();
    private SharedPreferences sharedPreferences;
    private final static String FILE_SP = "user_profile";
    private final static String SP_KEY_LOGIN = "u_login";
    private final BehaviorSubject<Boolean> mLoginStateSubject = BehaviorSubject.createDefault(false);
    private final BehaviorSubject<User> mUserStateSubject = BehaviorSubject.create();
    private final BehaviorSubject<UserUiState> mUserUIStateSubject = BehaviorSubject.createDefault(new UserUiState());
    public boolean isLogin(){
        return mUser != null;
    }
    public Observable<Boolean> subscribeLogin(){
        return mLoginStateSubject;
    }
    public Observable<User> subscribeUser(){
        return mUserStateSubject;
    }
    public Observable<UserUiState> subscribeUserUIState(){
        return mUserUIStateSubject;
    }
    /**
     * 用户登录成功
     * @param user
     */
    public void login(User user){
        if (mUser != null){
            logout();
        }
        mUser = user;
        mLoginStateSubject.onNext(true);
        UserUiState uiState = new UserUiState();
        uiState.isLogin = true;
        uiState.user = user;
        mUserUIStateSubject.onNext(uiState);
        savaUserProfile();
    }
    private void savaUserProfile(){
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(SP_KEY_LOGIN,gson.toJson(mUser));
        editor.apply();
    }
    /**
     * 用户更新
     * @param block
     */
    public void update(UpdateUserBlock block){
        block.onUpdate(mUser);
        UserUiState uiState = mUserUIStateSubject.getValue();
        uiState.user = mUser;
        uiState.isLogin = true;
        mUserUIStateSubject.onNext(uiState);
        savaUserProfile();
    }
    /**
     * 登出
     */
    public void logout(){
        mUser = null;
        mLoginStateSubject.onNext(false);
        mUserUIStateSubject.onNext(new UserUiState());
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.remove(SP_KEY_LOGIN);
        editor.apply();
    }
    private UserManager(Context mContext) {
        this.mContext = mContext;
        sharedPreferences = mContext.getSharedPreferences(FILE_SP, Context.MODE_PRIVATE);
        String cache = sharedPreferences.getString(SP_KEY_LOGIN,"");
        if (!cache.isEmpty()){
            this.mUser = gson.fromJson(cache,User.class);
            mLoginStateSubject.onNext(true);
            mUserStateSubject.onNext(mUser);
            UserUiState uiState = new UserUiState();
            uiState.isLogin = true;
            uiState.user = this.mUser;
            mUserUIStateSubject.onNext(uiState);
        }
    }

    public String token(){
        return mUser != null ? mUser.token:"";
    }
    public String unionId(){
        return mUser != null ? mUser.unionId:"";
    }
    public String userId(){
        return mUser != null ? mUser.userId:"";
    }
    private static UserManager INSTANCE = null;
    public static void init(Context context){
        INSTANCE = new UserManager(context);
    }
    public static UserManager getInstance() {
        return INSTANCE;
    }
    public interface UpdateUserBlock{
        void onUpdate(User user);
    }
}
